package com.lancoo.ccas53.arrangecourse.coursescheduling.assignlist;

import com.lancoo.ccas53.arrangecourse.coursescheduling.allocatetimeslotroom.AllocateTimeslotRoom;
import com.lancoo.ccas53.arrangecourse.entities.ClassHour;
import com.lancoo.ccas53.arrangecourse.entities.CourseUnit;
import com.lancoo.ccas53.arrangecourse.entities.TeachingClassUnit;
import com.lancoo.ccas53.arrangecourse.entities.TimeslotRoom;
import com.lancoo.ccas53.arrangecourse.rulecheck.RuleService;

import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description 无冲突排课
 * @Author Fortysun
 * @Date 2023/9/7 19:58
 **/
public class NoClashAssign extends NormalAssign {
    /**
     * 接收外面传入的排课规则，以及教室课时分组的分配规则；
     *
     * @param ruleService          排课规则检测类
     * @param allocateTimeslotRoom 教室课时分组分配处理；
     */
    public NoClashAssign(RuleService ruleService,
                         AllocateTimeslotRoom allocateTimeslotRoom) {
        super(ruleService, allocateTimeslotRoom);
    }

    /**
     * 生成无冲突的课表排课单元信息
     * 将教学班排课单元，转换为ClassHour课表课时对象，并添加到map，方便用来判断排课冲突以及后面的冲突计算
     * @param teachingClassUnit 教学班排课单元
     * @param classHourMap 已锁定的课表信息
     */
    @Override
    protected ConcurrentHashMap<String, CopyOnWriteArrayList<ClassHour>> GenerateClassHour(TeachingClassUnit teachingClassUnit,
                                             ConcurrentHashMap<String, CopyOnWriteArrayList<ClassHour>> classHourMap) {
        //获取分配的教室课时分组信息列表对象；
        LinkedList<TimeslotRoom> timeslotRoomList= teachingClassUnit.getTimeslotRoomList();
        //生成教学班排课单元信息，并添加到map
        for (TimeslotRoom timeslotRoom: timeslotRoomList) {
            //生成排课单元信息
            ClassHour classHour = new ClassHour();
            //为排课单元，添加教学班数据
            classHour.setCourseUnit(new CourseUnit(teachingClassUnit));
            //为排课单元，添加教室课时分组数据
            classHour.setTimeslotRoom(new TimeslotRoom(timeslotRoom));
            //获取分配的教室课时分组的，课时编码信息，如：0101代表周一第一节课，作为key，进行返回
            String key = timeslotRoom.getTimeslotCode();
            //将获取到的classHour存入map
            if (classHourMap.get(key) == null) {
                //创建排课单元列表信息
                CopyOnWriteArrayList<ClassHour> classHourList = new CopyOnWriteArrayList<>();
                //将生成的教学班排课单元，添加到列表
                classHourList.add(classHour);
                //将列表添加到map中
                classHourMap.put(key, classHourList);
            }
            else {
                //直接将排课单元信息，添加到map里指定的列表中
                classHourMap.get(key).add(classHour);
            }
        }

        return classHourMap;
    }
}
